package com.spic.business.riskbase.hierarchy.service.impl;

import java.util.List;
import com.spic.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.spic.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.spic.business.riskbase.hierarchy.domain.RiskWorkStep;
import com.spic.business.riskbase.hierarchy.mapper.RiskWorkTaskMapper;
import com.spic.business.riskbase.hierarchy.domain.RiskWorkTask;
import com.spic.business.riskbase.hierarchy.service.IRiskWorkTaskService;

/**
 * workTaskService业务层处理
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@Service
public class RiskWorkTaskServiceImpl implements IRiskWorkTaskService
{
    @Autowired
    private RiskWorkTaskMapper riskWorkTaskMapper;

    /**
     * 查询workTask
     *
     * @param id workTask主键
     * @return workTask
     */
    @Override
    public RiskWorkTask selectRiskWorkTaskById(Long id)
    {
        return riskWorkTaskMapper.selectRiskWorkTaskById(id);
    }

    /**
     * 查询workTask列表
     *
     * @param riskWorkTask workTask
     * @return workTask
     */
    @Override
    public List<RiskWorkTask> selectRiskWorkTaskList(RiskWorkTask riskWorkTask)
    {
        return riskWorkTaskMapper.selectRiskWorkTaskList(riskWorkTask);
    }

    /**
     * 新增workTask
     *
     * @param riskWorkTask workTask
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRiskWorkTask(RiskWorkTask riskWorkTask)
    {
        int rows = riskWorkTaskMapper.insertRiskWorkTask(riskWorkTask);
        insertRiskWorkStep(riskWorkTask);
        return rows;
    }

    /**
     * 修改workTask
     *
     * @param riskWorkTask workTask
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRiskWorkTask(RiskWorkTask riskWorkTask)
    {
        riskWorkTaskMapper.deleteRiskWorkStepByWorkTaskId(riskWorkTask.getId());
        insertRiskWorkStep(riskWorkTask);
        return riskWorkTaskMapper.updateRiskWorkTask(riskWorkTask);
    }

    /**
     * 批量删除workTask
     *
     * @param ids 需要删除的workTask主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskWorkTaskByIds(Long[] ids)
    {
        riskWorkTaskMapper.deleteRiskWorkStepByWorkTaskIds(ids);
        return riskWorkTaskMapper.deleteRiskWorkTaskByIds(ids);
    }

    /**
     * 删除workTask信息
     *
     * @param id workTask主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskWorkTaskById(Long id)
    {
        riskWorkTaskMapper.deleteRiskWorkStepByWorkTaskId(id);
        return riskWorkTaskMapper.deleteRiskWorkTaskById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param riskWorkTask workTask对象
     */
    public void insertRiskWorkStep(RiskWorkTask riskWorkTask)
    {
        List<RiskWorkStep> riskWorkStepList = riskWorkTask.getRiskWorkStepList();
        Long id = riskWorkTask.getId();
        if (StringUtils.isNotNull(riskWorkStepList))
        {
            List<RiskWorkStep> list = new ArrayList<RiskWorkStep>();
            for (RiskWorkStep riskWorkStep : riskWorkStepList)
            {
                riskWorkStep.setWorkTaskId(id);
                list.add(riskWorkStep);
            }
            if (list.size() > 0)
            {
                riskWorkTaskMapper.batchRiskWorkStep(list);
            }
        }
    }
}
